package plugin;

import java.util.ArrayList;
import java.util.Iterator;

public class MediaPlugin extends PluginBase {

    private double media;
    private ArrayList<Double> numeros;
    private MediaView view;

    public MediaPlugin(ArrayList<Double> numeros) throws Exception {
        super(numeros);
    }

    @Override
    public void setDados(ArrayList<Double> numeros) {
        this.numeros = numeros;
        calculaMedia();
    }

    private void calculaMedia() {
        double total = 0;
        for (Iterator<Double> it = numeros.iterator(); it.hasNext();) {
            total += it.next().doubleValue();
        }
        media = total / numeros.size();
    }

    @Override
    public void preprocessa() {
    }

    @Override
    public void posprocessa() {
        view = new MediaView();
        view.getTxtResultado().setText(Double.toString(media));
        view.getLstNumeros().setListData(numeros.toArray());
        view.setVisible(true);
    }
}
